<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>NeuZephyr: nz::nodes::calc::SubNode Class Reference</title>
<link rel="icon" href="NZ_logo2.png" type="image/x-icon" />
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="NZ_logo2.png"/></td>
  <td id="projectalign">
   <div id="projectname">NeuZephyr
   </div>
   <div id="projectbrief">Simple DL Framework</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li class="current"><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>Class&#160;List</span></a></li>
      <li><a href="classes.html"><span>Class&#160;Index</span></a></li>
      <li><a href="inherits.html"><span>Class&#160;Hierarchy</span></a></li>
      <li><a href="functions.html"><span>Class&#160;Members</span></a></li>
    </ul>
  </div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){ initResizable(false); });
/* @license-end */
</script>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><b>nz</b></li><li class="navelem"><a class="el" href="namespacenz_1_1nodes.html">nodes</a></li><li class="navelem"><a class="el" href="namespacenz_1_1nodes_1_1calc.html">calc</a></li><li class="navelem"><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html">SubNode</a></li>  </ul>
</div>
</div><!-- top -->
<div id="doc-content">
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public Member Functions</a> &#124;
<a href="classnz_1_1nodes_1_1calc_1_1_sub_node-members.html">List of all members</a>  </div>
  <div class="headertitle"><div class="title">nz::nodes::calc::SubNode Class Reference</div></div>
</div><!--header-->
<div class="contents">

<p>Represents a subtraction operation node in a computational graph.  
 <a href="#details">More...</a></p>
<div class="dynheader">
Inheritance diagram for nz::nodes::calc::SubNode:</div>
<div class="dyncontent">
<div class="center"><img src="classnz_1_1nodes_1_1calc_1_1_sub_node__inherit__graph.png" border="0" usemap="#anz_1_1nodes_1_1calc_1_1_sub_node_inherit__map" alt="Inheritance graph"/></div>
<map name="anz_1_1nodes_1_1calc_1_1_sub_node_inherit__map" id="anz_1_1nodes_1_1calc_1_1_sub_node_inherit__map">
<area shape="rect" title="Represents a subtraction operation node in a computational graph." alt="" coords="5,80,178,107"/>
<area shape="rect" href="classnz_1_1nodes_1_1_node.html" title="Base class for nodes in a neural network or computational graph." alt="" coords="33,5,150,32"/>
<area shape="poly" title=" " alt="" coords="94,48,94,80,89,80,89,48"/>
</map>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<div class="dynheader">
Collaboration diagram for nz::nodes::calc::SubNode:</div>
<div class="dyncontent">
<div class="center"><img src="classnz_1_1nodes_1_1calc_1_1_sub_node__coll__graph.png" border="0" usemap="#anz_1_1nodes_1_1calc_1_1_sub_node_coll__map" alt="Collaboration graph"/></div>
<map name="anz_1_1nodes_1_1calc_1_1_sub_node_coll__map" id="anz_1_1nodes_1_1calc_1_1_sub_node_coll__map">
<area shape="rect" title="Represents a subtraction operation node in a computational graph." alt="" coords="5,80,178,107"/>
<area shape="rect" href="classnz_1_1nodes_1_1_node.html" title="Base class for nodes in a neural network or computational graph." alt="" coords="33,5,150,32"/>
<area shape="poly" title=" " alt="" coords="94,48,94,80,89,80,89,48"/>
</map>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="pub-methods" name="pub-methods"></a>
Public Member Functions</h2></td></tr>
<tr class="memitem:ab5c38bfc256e7784a22bb2bdcab7a72d" id="r_ab5c38bfc256e7784a22bb2bdcab7a72d"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ab5c38bfc256e7784a22bb2bdcab7a72d">SubNode</a> (<a class="el" href="classnz_1_1nodes_1_1_node.html">Node</a> *input_left, <a class="el" href="classnz_1_1nodes_1_1_node.html">Node</a> *input_right)</td></tr>
<tr class="memdesc:ab5c38bfc256e7784a22bb2bdcab7a72d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Constructor to initialize a <code><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html" title="Represents a subtraction operation node in a computational graph.">SubNode</a></code> for tensor subtraction.  <br /></td></tr>
<tr class="separator:ab5c38bfc256e7784a22bb2bdcab7a72d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a6ba6a63da4e869f8f0004896d01fe3f1" id="r_a6ba6a63da4e869f8f0004896d01fe3f1"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a6ba6a63da4e869f8f0004896d01fe3f1">forward</a> () override</td></tr>
<tr class="memdesc:a6ba6a63da4e869f8f0004896d01fe3f1"><td class="mdescLeft">&#160;</td><td class="mdescRight">Forward pass for the <code><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html" title="Represents a subtraction operation node in a computational graph.">SubNode</a></code> to perform tensor subtraction.  <br /></td></tr>
<tr class="separator:a6ba6a63da4e869f8f0004896d01fe3f1"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:abb396a092ac9fb09c3a656329132842d" id="r_abb396a092ac9fb09c3a656329132842d"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#abb396a092ac9fb09c3a656329132842d">backward</a> () override</td></tr>
<tr class="memdesc:abb396a092ac9fb09c3a656329132842d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Backward pass for the <code><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html" title="Represents a subtraction operation node in a computational graph.">SubNode</a></code> to propagate gradients.  <br /></td></tr>
<tr class="separator:abb396a092ac9fb09c3a656329132842d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="inherit_header pub_methods_classnz_1_1nodes_1_1_node"><td colspan="2" onclick="javascript:dynsection.toggleInherit('pub_methods_classnz_1_1nodes_1_1_node')"><img src="closed.png" alt="-"/>&#160;Public Member Functions inherited from <a class="el" href="classnz_1_1nodes_1_1_node.html">nz::nodes::Node</a></td></tr>
<tr class="memitem:a687ee9c34eb61f8f28caa201ca42696e inherit pub_methods_classnz_1_1nodes_1_1_node" id="r_a687ee9c34eb61f8f28caa201ca42696e"><td class="memItemLeft" align="right" valign="top">virtual void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classnz_1_1nodes_1_1_node.html#a687ee9c34eb61f8f28caa201ca42696e">print</a> (std::ostream &amp;os) const</td></tr>
<tr class="memdesc:a687ee9c34eb61f8f28caa201ca42696e inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Prints the type, data, and gradient of the node.  <br /></td></tr>
<tr class="separator:a687ee9c34eb61f8f28caa201ca42696e inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a9b85913e12422bb4ac2fff483427bb47 inherit pub_methods_classnz_1_1nodes_1_1_node" id="r_a9b85913e12422bb4ac2fff483427bb47"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classnz_1_1nodes_1_1_node.html#a9b85913e12422bb4ac2fff483427bb47">dataInject</a> (Tensor::value_type *data, bool grad=false) const</td></tr>
<tr class="memdesc:a9b85913e12422bb4ac2fff483427bb47 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Injects data into a relevant tensor object, optionally setting its gradient requirement.  <br /></td></tr>
<tr class="separator:a9b85913e12422bb4ac2fff483427bb47 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a609f1730085dd1d31e0ddcbbae48a065 inherit pub_methods_classnz_1_1nodes_1_1_node" id="r_a609f1730085dd1d31e0ddcbbae48a065"><td class="memTemplParams" colspan="2">template&lt;typename Iterator &gt; </td></tr>
<tr class="memitem:a609f1730085dd1d31e0ddcbbae48a065 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memTemplItemLeft" align="right" valign="top">void&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="classnz_1_1nodes_1_1_node.html#a609f1730085dd1d31e0ddcbbae48a065">dataInject</a> (Iterator begin, Iterator end, const bool grad=false) const</td></tr>
<tr class="memdesc:a609f1730085dd1d31e0ddcbbae48a065 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Injects data from an iterator range into the output tensor of the InputNode, optionally setting its gradient requirement.  <br /></td></tr>
<tr class="separator:a609f1730085dd1d31e0ddcbbae48a065 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af8b4bab3271df92ca1f0914f7a97b1e8 inherit pub_methods_classnz_1_1nodes_1_1_node" id="r_af8b4bab3271df92ca1f0914f7a97b1e8"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classnz_1_1nodes_1_1_node.html#af8b4bab3271df92ca1f0914f7a97b1e8">dataInject</a> (const std::initializer_list&lt; Tensor::value_type &gt; &amp;data, bool grad=false) const</td></tr>
<tr class="memdesc:af8b4bab3271df92ca1f0914f7a97b1e8 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Injects data from a std::initializer_list into the output tensor of the <a class="el" href="classnz_1_1nodes_1_1_node.html" title="Base class for nodes in a neural network or computational graph.">Node</a>, optionally setting its gradient requirement.  <br /></td></tr>
<tr class="separator:af8b4bab3271df92ca1f0914f7a97b1e8 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Represents a subtraction operation node in a computational graph. </p>
<p>The <code><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html" title="Represents a subtraction operation node in a computational graph.">SubNode</a></code> class performs element-wise subtraction between two input tensors. Unlike scalar-based operations, this node handles tensor-to-tensor subtraction, ensuring compatibility of input shapes and propagating gradients for both input tensors during backpropagation.</p>
<p>Key features:</p><ul>
<li><b>Forward Pass</b>: Computes the element-wise subtraction of two input tensors and stores the result in the <code>output</code> tensor.</li>
<li><b>Backward Pass</b>: Propagates gradients for both input tensors. For the left input tensor, the gradient is directly copied from the <code>output</code> tensor's gradient. For the right input tensor, the gradient is negated before being propagated.</li>
<li><b>Shape Validation</b>: Ensures the shapes of the two input tensors are identical during construction. Mismatched shapes result in an exception.</li>
<li><b>Gradient Management</b>: Tracks whether gradients are required for either of the input tensors, and propagates gradients accordingly.</li>
</ul>
<p>This class is part of the <code><a class="el" href="namespacenz_1_1nodes.html" title="Contains classes and functionality for nodes in a neural network or computational graph.">nz::nodes</a></code> namespace and facilitates tensor-to-tensor subtraction operations in computational graphs.</p>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>The left and right input tensors must have the same shape; otherwise, an exception will be thrown.</li>
<li>Gradients are propagated efficiently, with negation applied to the right input tensor's gradient during backpropagation.</li>
</ul>
</dd></dl>
<h3><a class="anchor" id="autotoc_md95"></a>
Usage Example:</h3>
<div class="fragment"><div class="line"><span class="comment">// Example: Using SubNode for tensor subtraction</span></div>
<div class="line">InputNode input1({3, 3}, <span class="keyword">true</span>);  <span class="comment">// Create the first input node with shape {3, 3}</span></div>
<div class="line">InputNode input2({3, 3}, <span class="keyword">true</span>);  <span class="comment">// Create the second input node with shape {3, 3}</span></div>
<div class="line"> </div>
<div class="line">input1.output-&gt;fill(5.0f);  <span class="comment">// Fill the first tensor with value 5.0</span></div>
<div class="line">input2.output-&gt;fill(3.0f);  <span class="comment">// Fill the second tensor with value 3.0</span></div>
<div class="line"> </div>
<div class="line"><a class="code hl_function" href="#ab5c38bfc256e7784a22bb2bdcab7a72d">SubNode</a> sub_node(&amp;input1, &amp;input2);  <span class="comment">// Subtract input2 from input1</span></div>
<div class="line">sub_node.forward();  <span class="comment">// Perform the forward pass</span></div>
<div class="line">sub_node.backward();  <span class="comment">// Propagate gradients in the backward pass</span></div>
<div class="line"> </div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;Output: &quot;</span> &lt;&lt; *sub_node.output &lt;&lt; std::endl;  <span class="comment">// Print the result</span></div>
<div class="ttc" id="aclassnz_1_1nodes_1_1calc_1_1_sub_node_html_ab5c38bfc256e7784a22bb2bdcab7a72d"><div class="ttname"><a href="#ab5c38bfc256e7784a22bb2bdcab7a72d">nz::nodes::calc::SubNode::SubNode</a></div><div class="ttdeci">SubNode(Node *input_left, Node *input_right)</div><div class="ttdoc">Constructor to initialize a SubNode for tensor subtraction.</div><div class="ttdef"><b>Definition</b> <a href="_nodes_8cu_source.html#l00283">Nodes.cu:283</a></div></div>
</div><!-- fragment --><dl class="section see"><dt>See also</dt><dd><a class="el" href="#a6ba6a63da4e869f8f0004896d01fe3f1" title="Forward pass for the SubNode to perform tensor subtraction.">forward()</a> for the tensor subtraction computation in the <a class="el" href="#a6ba6a63da4e869f8f0004896d01fe3f1" title="Forward pass for the SubNode to perform tensor subtraction.">forward</a> pass. </dd>
<dd>
<a class="el" href="#abb396a092ac9fb09c3a656329132842d" title="Backward pass for the SubNode to propagate gradients.">backward()</a> for gradient propagation in the <a class="el" href="#abb396a092ac9fb09c3a656329132842d" title="Backward pass for the SubNode to propagate gradients.">backward</a> pass.</dd></dl>
<dl class="exception"><dt>Exceptions</dt><dd>
  <table class="exception">
    <tr><td class="paramname">std::invalid_argument</td><td>If the shapes of the two input tensors are not identical.</td></tr>
  </table>
  </dd>
</dl>
<dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/12/05 </dd></dl>

<p class="definition">Definition at line <a class="el" href="_nodes_8cuh_source.html#l01787">1787</a> of file <a class="el" href="_nodes_8cuh_source.html">Nodes.cuh</a>.</p>
</div><h2 class="groupheader">Constructor &amp; Destructor Documentation</h2>
<a id="ab5c38bfc256e7784a22bb2bdcab7a72d" name="ab5c38bfc256e7784a22bb2bdcab7a72d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ab5c38bfc256e7784a22bb2bdcab7a72d">&#9670;&#160;</a></span>SubNode()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">nz::nodes::calc::SubNode::SubNode </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="classnz_1_1nodes_1_1_node.html">Node</a> *</td>          <td class="paramname"><span class="paramname"><em>input_left</em></span>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="classnz_1_1nodes_1_1_node.html">Node</a> *</td>          <td class="paramname"><span class="paramname"><em>input_right</em></span>&#160;)</td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Constructor to initialize a <code><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html" title="Represents a subtraction operation node in a computational graph.">SubNode</a></code> for tensor subtraction. </p>
<p>The constructor initializes a <code><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html" title="Represents a subtraction operation node in a computational graph.">SubNode</a></code>, which performs element-wise subtraction between two input tensors. It validates the shapes of the input tensors to ensure they are compatible for subtraction, establishes connections to the input nodes, and prepares the output tensor for storing the results.</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">input_left</td><td>A pointer to the first input node. Its <code>output</code> tensor is treated as the left operand in the subtraction. </td></tr>
    <tr><td class="paramname">input_right</td><td>A pointer to the second input node. Its <code>output</code> tensor is treated as the right operand in the subtraction.</td></tr>
  </table>
  </dd>
</dl>
<ul>
<li>The constructor validates that the shapes of the two input tensors are identical. If the shapes do not match, an exception is thrown to prevent invalid operations.</li>
<li>The input nodes are added to the <code>inputs</code> vector, establishing their connection in the computational graph.</li>
<li>The <code>output</code> tensor is initialized with the same shape as the input tensors, and its gradient tracking is determined based on the requirements of the input tensors.</li>
<li>The node's type is set to <code>"Sub"</code> to reflect its operation.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>The input tensors must have the same shape; mismatched shapes result in an exception.</li>
<li>This node supports automatic gradient tracking if either input tensor requires gradients.</li>
</ul>
</dd></dl>
<dl class="exception"><dt>Exceptions</dt><dd>
  <table class="exception">
    <tr><td class="paramname">std::invalid_argument</td><td>If the shapes of the two input tensors are not identical.</td></tr>
  </table>
  </dd>
</dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="#a6ba6a63da4e869f8f0004896d01fe3f1" title="Forward pass for the SubNode to perform tensor subtraction.">forward()</a> for the <a class="el" href="#a6ba6a63da4e869f8f0004896d01fe3f1" title="Forward pass for the SubNode to perform tensor subtraction.">forward</a> pass implementation. </dd>
<dd>
<a class="el" href="#abb396a092ac9fb09c3a656329132842d" title="Backward pass for the SubNode to propagate gradients.">backward()</a> for gradient propagation in the <a class="el" href="#abb396a092ac9fb09c3a656329132842d" title="Backward pass for the SubNode to propagate gradients.">backward</a> pass.</dd></dl>
<dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/12/05 </dd></dl>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00283">283</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>

</div>
</div>
<h2 class="groupheader">Member Function Documentation</h2>
<a id="abb396a092ac9fb09c3a656329132842d" name="abb396a092ac9fb09c3a656329132842d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#abb396a092ac9fb09c3a656329132842d">&#9670;&#160;</a></span>backward()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">void nz::nodes::calc::SubNode::backward </td>
          <td>(</td>
          <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">override</span><span class="mlabel">virtual</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Backward pass for the <code><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html" title="Represents a subtraction operation node in a computational graph.">SubNode</a></code> to propagate gradients. </p>
<p>The <code><a class="el" href="#abb396a092ac9fb09c3a656329132842d" title="Backward pass for the SubNode to propagate gradients.">backward()</a></code> method computes and propagates the gradients of the loss with respect to both input tensors. For the left input tensor, the gradient is directly copied from the <code>output</code> tensor's gradient. For the right input tensor, the gradient is negated before propagation.</p>
<ul>
<li>If the left input tensor requires gradients, the gradient from the <code>output</code> tensor is directly copied to the gradient tensor of the left input.</li>
<li>If the right input tensor requires gradients, a temporary buffer is allocated, and the gradient of the <code>output</code> tensor is negated using a CUDA kernel (<code>Negation</code>) before being copied to the gradient tensor of the right input.</li>
<li>Temporary GPU memory (<code>n_grad</code>) is managed within the method and is released after use.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>Gradient computation is efficient and ensures proper handling for both input tensors.</li>
<li>The negation step for the right input tensor is necessary due to the derivative of subtraction with respect to the right operand being -1.</li>
</ul>
</dd></dl>
<dl class="section warning"><dt>Warning</dt><dd><ul>
<li>Proper memory management is ensured by freeing the temporary buffer (<code>n_grad</code>) after use.</li>
</ul>
</dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="#a6ba6a63da4e869f8f0004896d01fe3f1" title="Forward pass for the SubNode to perform tensor subtraction.">forward()</a> for the tensor subtraction computation in the <a class="el" href="#a6ba6a63da4e869f8f0004896d01fe3f1" title="Forward pass for the SubNode to perform tensor subtraction.">forward</a> pass.</dd></dl>
<dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/12/05 </dd></dl>

<p>Implements <a class="el" href="classnz_1_1nodes_1_1_node.html#a0a9ecbaa3d790ba38e8218aca7837fd0">nz::nodes::Node</a>.</p>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00302">302</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>
<div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="classnz_1_1nodes_1_1calc_1_1_sub_node_abb396a092ac9fb09c3a656329132842d_cgraph.png" border="0" usemap="#aclassnz_1_1nodes_1_1calc_1_1_sub_node_abb396a092ac9fb09c3a656329132842d_cgraph" alt=""/></div>
<map name="aclassnz_1_1nodes_1_1calc_1_1_sub_node_abb396a092ac9fb09c3a656329132842d_cgraph" id="aclassnz_1_1nodes_1_1calc_1_1_sub_node_abb396a092ac9fb09c3a656329132842d_cgraph">
<area shape="rect" title="Backward pass for the SubNode to propagate gradients." alt="" coords="5,113,178,156"/>
<area shape="rect" href="namespacenz_1_1krnl.html#a0ed44a68bfb86a9fd3d6c3b25614713f" title="Copies gradient data from one array to another with specified offsets." alt="" coords="254,96,382,123"/>
<area shape="poly" title=" " alt="" coords="178,122,239,115,239,121,179,128"/>
<area shape="rect" href="classnz_1_1cu_strm_1_1_stream_manager.html#ab4b2eb422e0e1ee44bdfdc0eb94457ce" title="Returns a reference to the singleton instance of the StreamManager." alt="" coords="459,205,644,248"/>
<area shape="poly" title=" " alt="" coords="143,154,226,183,338,204,444,217,443,222,337,210,225,188,141,159"/>
<area shape="rect" href="classnz_1_1cu_strm_1_1_stream_manager.html#afa38d5c6db0e6b48c8f74ce8ad0df2bc" title="Asynchronously copies data between CUDA device and host memory based on the specified memory copy kin..." alt="" coords="226,29,411,72"/>
<area shape="poly" title=" " alt="" coords="149,110,225,82,241,75,243,80,227,86,151,115"/>
<area shape="rect" href="namespacenz_1_1krnl.html#a9ac0590fbb5eb7f51b05da574e9845a8" title="Copies gradient data from one array to another with specified offsets." alt="" coords="249,147,388,173"/>
<area shape="poly" title=" " alt="" coords="179,142,234,148,233,153,178,147"/>
<area shape="rect" href="classnz_1_1cu_strm_1_1_stream_manager.html#a1de1cf3aadea137faf90a2f9b4b7abe2" title="Acquires CUDA stream from pool using round&#45;robin scheduling." alt="" coords="459,5,644,48"/>
<area shape="poly" title=" " alt="" coords="376,93,410,82,458,58,473,52,475,56,460,62,412,86,378,98"/>
<area shape="poly" title=" " alt="" coords="384,119,412,132,429,147,438,162,446,177,461,191,467,195,464,200,457,195,442,180,433,165,424,151,410,137,382,124"/>
<area shape="rect" href="classnz_1_1cu_strm_1_1_stream_manager.html#a1260d95d0eddf75b72700da07361a4bd" title="Records write completion event for asynchronous data operations." alt="" coords="459,139,644,181"/>
<area shape="poly" title=" " alt="" coords="383,121,445,134,443,139,382,126"/>
<area shape="rect" href="classnz_1_1cu_strm_1_1_stream_manager.html#adb1078a67c6e38932d7d58c2adb05ec0" title="Synchronizes CUDA stream execution until data writes complete." alt="" coords="459,72,644,115"/>
<area shape="poly" title=" " alt="" coords="383,102,443,98,444,103,383,108"/>
<area shape="poly" title=" " alt="" coords="411,38,443,35,444,40,412,44"/>
<area shape="poly" title=" " alt="" coords="412,65,444,71,443,76,411,70"/>
<area shape="poly" title=" " alt="" coords="387,145,409,133,425,115,433,97,441,77,457,58,462,54,465,59,461,62,445,80,438,99,430,118,413,137,389,149"/>
<area shape="poly" title=" " alt="" coords="369,171,460,198,459,203,367,177"/>
<area shape="poly" title=" " alt="" coords="388,157,443,157,443,163,388,163"/>
<area shape="poly" title=" " alt="" coords="370,144,410,132,462,117,464,122,412,137,371,149"/>
</map>
</div>

</div>
</div>
<a id="a6ba6a63da4e869f8f0004896d01fe3f1" name="a6ba6a63da4e869f8f0004896d01fe3f1"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a6ba6a63da4e869f8f0004896d01fe3f1">&#9670;&#160;</a></span>forward()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">void nz::nodes::calc::SubNode::forward </td>
          <td>(</td>
          <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">override</span><span class="mlabel">virtual</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Forward pass for the <code><a class="el" href="classnz_1_1nodes_1_1calc_1_1_sub_node.html" title="Represents a subtraction operation node in a computational graph.">SubNode</a></code> to perform tensor subtraction. </p>
<p>The <code><a class="el" href="#a6ba6a63da4e869f8f0004896d01fe3f1" title="Forward pass for the SubNode to perform tensor subtraction.">forward()</a></code> method computes the element-wise subtraction between the two input tensors. It uses a CUDA kernel to perform the operation efficiently on the GPU, storing the result in the <code>output</code> tensor.</p>
<ul>
<li>A CUDA kernel (<code>MatrixSub</code>) is launched to subtract the elements of the second input tensor (<code>inputs[1]</code>) from the corresponding elements of the first input tensor (<code>inputs[0]</code>).</li>
<li>The grid and block dimensions are calculated dynamically based on the size of the <code>output</code> tensor to ensure optimal GPU parallelism.</li>
<li>The result of the subtraction is stored in the <code>output</code> tensor.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>The subtraction operation is performed as <code>output[i] = input_left[i] - input_right[i]</code> for each element.</li>
<li>Ensure the shapes of the input tensors are identical; this is enforced during node construction.</li>
</ul>
</dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="#abb396a092ac9fb09c3a656329132842d" title="Backward pass for the SubNode to propagate gradients.">backward()</a> for gradient propagation in the <a class="el" href="#abb396a092ac9fb09c3a656329132842d" title="Backward pass for the SubNode to propagate gradients.">backward</a> pass.</dd></dl>
<dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/12/05 </dd></dl>

<p>Implements <a class="el" href="classnz_1_1nodes_1_1_node.html#a8a828c2e91a4aa2a9ab7b94554e4685b">nz::nodes::Node</a>.</p>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00298">298</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>
<div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="classnz_1_1nodes_1_1calc_1_1_sub_node_a6ba6a63da4e869f8f0004896d01fe3f1_cgraph.png" border="0" usemap="#aclassnz_1_1nodes_1_1calc_1_1_sub_node_a6ba6a63da4e869f8f0004896d01fe3f1_cgraph" alt=""/></div>
<map name="aclassnz_1_1nodes_1_1calc_1_1_sub_node_a6ba6a63da4e869f8f0004896d01fe3f1_cgraph" id="aclassnz_1_1nodes_1_1calc_1_1_sub_node_a6ba6a63da4e869f8f0004896d01fe3f1_cgraph">
<area shape="rect" title="Forward pass for the SubNode to perform tensor subtraction." alt="" coords="5,5,178,48"/>
<area shape="rect" href="namespacenz_1_1data.html#a7503b6894e8052ed54eb169550d135c0" title="Performs matrix subtraction operation on tensors with broadcast compatibility." alt="" coords="226,13,397,40"/>
<area shape="poly" title=" " alt="" coords="178,24,210,24,210,29,178,29"/>
</map>
</div>

</div>
</div>
<hr/>The documentation for this class was generated from the following files:<ul>
<li>D:/Users/Mgepahmge/Documents/C Program/NeuZephyr/include/NeuZephyr/<a class="el" href="_nodes_8cuh_source.html">Nodes.cuh</a></li>
<li>D:/Users/Mgepahmge/Documents/C Program/NeuZephyr/src/<a class="el" href="_nodes_8cu_source.html">Nodes.cu</a></li>
</ul>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0
</small></address>
</div><!-- doc-content -->
</body>
</html>
